﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.DbHelper
{
   public class DbHelperMySQL
    {
         public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
         public DbHelperMySQL()
         {
         }
         /// <summary>
         /// 执行查询语句，返回DataSet
         /// </summary>
         /// <param name="SQLString">查询语句</param>
         /// <returns>DataSet</returns>
         public static DataSet Query(string SQLString)
         {
             using (OleDbConnection connection = new OleDbConnection(connectionString))
             {
                 DataSet ds = new DataSet();
                 try
                 {
                     connection.Open();
                     OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
                     command.Fill(ds, "ds");
                 }
                 catch (OleDbException ex)
                 {
                     throw new Exception(ex.Message);
                 }
                 return ds;
             }
         }
         /// <summary>
         /// 执行一条计算查询结果语句，返回查询结果（object）。
         /// </summary>
         /// <param name="SQLString">计算查询结果语句</param>
         /// <returns>查询结果（object）</returns>
         public static object GetSingle(string SQLString)
         {
             using (OleDbConnection connection = new OleDbConnection(connectionString))
             {
                 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
                 {
                     try
                     {
                         connection.Open();
                         object obj = cmd.ExecuteScalar();
                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                         {
                             return null;
                         }
                         else
                         {
                             return obj;
                         }
                     }
                     catch (OleDbException e)
                     {
                         connection.Close();
                         throw e;
                     }
                 }
             }
         }
         public static int ExecuteSql(string SQLString)
         {
             using (OleDbConnection connection = new OleDbConnection(connectionString))
             {
                 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
                 {
                     try
                     {
                         connection.Open();
                         int rows = cmd.ExecuteNonQuery();
                         return rows;
                     }
                     catch (Exception e)
                     {
                         connection.Close();
                         throw e;
                     }
                 }
             }
         }
   }
}
